
package com.kanjian.music.fragment;

import com.android.volley.Response;
import com.android.volley.toolbox.ImageRequest;
import com.kanjian.music.R;
import com.kanjian.music.activity.BaseActivity;
import com.kanjian.music.activity.MusicianHomeActivity;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.entity.Music;
import com.kanjian.music.network.SimpleHttpRequestUtil;
import com.kanjian.music.util.PlayerUtil;
import com.kanjian.music.view.CircleImageView;
import com.kanjian.music.view.RecyclingImageView;
import com.kanjian.music.volley.VolleyQueue;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MusicPlayerRadioMusicInfoFragment extends BaseFragment {

    public static MusicPlayerRadioMusicInfoFragment newInstance() {
        return new MusicPlayerRadioMusicInfoFragment();
    }

    private View mRootView;

    private CircleImageView mIv_musicPic;

    private TextView mTv_musicName;

    private TextView mTv_musicianName;

    private Button mBtn_del;

    private Button mBtn_musicianHome;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        initView(inflater, container);
        return mRootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_musicplayer_raido_musicinfo, container,
                    false);
            mIv_musicPic = (CircleImageView) mRootView
                    .findViewById(R.id.music_player_radio_music_info_pic);
            mTv_musicName = (TextView) mRootView
                    .findViewById(R.id.music_player_radio_music_info_music_name);
            mTv_musicianName = (TextView) mRootView
                    .findViewById(R.id.music_player_radio_music_info_musician_name);
            final Music music = PlayerUtil.getCurrentPlayingMusic();
            if (music != null) {
                mTv_musicName.setText(music.getMusicName());
                mTv_musicianName.setText(music.getUserName());
            }
            mBtn_del = (Button) mRootView.findViewById(R.id.music_player_radio_music_info_del);
            mBtn_del.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    PlayerUtil.removeCurrentMusic();
                    ((BaseActivity) getActivity()).mPlayService.next(true);
                    SimpleHttpRequestUtil.doDelRadioMusic(PlayerUtil.getCurrentPlayingMusic()
                            .getMusicId(), PlayerUtil.mCurrentRadioType);
                }
            });
            mBtn_musicianHome = (Button) mRootView
                    .findViewById(R.id.music_player_radio_music_info_musician_home);
            mBtn_musicianHome.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), MusicianHomeActivity.class);
                    intent.putExtra(IntentConstants.MUSICIANID, music.getUserId());
                    startActivity(intent);
                }
            });
            refreshMusicPic();
        }
    }

    public void refreshMusicPic() {
        Music music = PlayerUtil.getCurrentPlayingMusic();
        if (music != null && mTv_musicName != null && mTv_musicianName != null) {
            mTv_musicName.setText(music.getMusicName());
            mTv_musicianName.setText(music.getUserName());
            VolleyQueue.getImageRequestQueue().add(
                    new ImageRequest(music.getUserPic(), new Response.Listener<Bitmap>() {
                        @Override
                        public void onResponse(Bitmap bmp) {
                            if (mIv_musicPic != null) {
                                mIv_musicPic.setImageBitmap(bmp);
                            }
                        }
                    }, 0, 0, Config.ARGB_8888, null));
        }
    }

}
